<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/brands' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column align="center" label="ID" prop="id" width="60"></el-table-column>
      <el-table-column align="center" label="LOGO" prop="logo" width="120"></el-table-column>
      <el-table-column align="center" label="名称" prop="name" width="120"></el-table-column>
      <el-table-column align="center" label="拼音" prop="pinyin" width="180"></el-table-column>
      <el-table-column align="center" label="简介" prop="description"></el-table-column>
      <el-table-column align="center" label="排序序号" prop="sort" width="80"></el-table-column>
      <el-table-column align="center" label="操作" width="100">
        <template slot-scope="scope">
          <el-button circle icon="el-icon-edit" size="mini" type="primary"
                     @click="handleEdit(scope.row)"></el-button>
          <el-button circle icon="el-icon-delete" size="mini" type="danger"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
        :before-close="handleClose"
        :visible.sync="dialogVisible"
        title="提示"
        width="100%">
            <span>
              <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="130px">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="拼音" prop="pinyin">
                  <el-input v-model="ruleForm.pinyin"></el-input>
                </el-form-item>
                <el-form-item label="LOGO" prop="logo">
                  <el-input v-model="ruleForm.logo"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="description">
                  <el-input v-model="ruleForm.description"></el-input>
                </el-form-item>
                <el-form-item label="关键词列表" prop="keywords">
                  <el-input v-model="ruleForm.keywords"></el-input>
                </el-form-item>
                <el-form-item label="排序序号" prop="sort">
                  <el-input v-model="ruleForm.sort"></el-input>
                </el-form-item>
                <el-form-item label="是否启用" prop="enable">
                  <el-switch
                    v-model="ruleForm.enable"
                    :active-value="1"
                    :inactive-value="0"
                    active-color="#13ce66"
                    inactive-color="#999">
                  </el-switch>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm(brand)">修改</el-button>
                  <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
              </el-form>
            </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      ruleForm: {
        name: '',
        pinyin: '',
        description: '',
        logo: '',
        enable: 1,
        keywords: '',
        sort: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入品牌拼音', trigger: 'blur'},
          {min: 4, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入品牌简介', trigger: 'blur'},
          {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
        ],
        sort: [
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      },
      brand: {}
    };
  },
  methods: {
    handleClose(){
      this.dialogVisible = false;
    },
    loadBrandList() {
      let url = 'http://localhost:8090/brands';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.code == 2000) {
          this.tableData = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    handleEdit(brand) {
     this.dialogVisible = true;
     this.brand = brand;
    },
    openDeleteConfirm(brand) {
      let title = '提示';
      let message = '此操作将永久删除【' + brand.name + '】品牌，是否继续？';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
      });
    },
    handleDelete(brand) {
      let url = 'http://localhost:8090/brands/' + brand.id + '/delete';
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.code != 2000) {
          this.$message.error(responseBody.message);
        }
        this.loadBrandList();
      });
    },
    submitForm(brand) {
      this.$refs.ruleForm.validate(valid => {
        console.log(valid)
        if (valid === true) {
          // 调后台登陆接口
          let url = 'http://localhost:8090/brands/'+brand.id+'/update';
          console.log('url = ' + url);
          this.axios.post(url, this.ruleForm).then((response) => {
            let responseBody = response.data;
            if (responseBody.code == 2000) {
              this.loadBrandList();
              this.dialogVisible = false;
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
            } else {
              this.$message.error(responseBody.message);
            }
          });
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadBrandList();
  }
}
</script>

<style scoped>

</style>